package cfca.seal.dao.main.impl;

import cfca.seal.bean.SealImage;
import cfca.seal.common.Page;
import cfca.seal.dao.PageDao;
import cfca.seal.dao.SmartDao;
import cfca.seal.dao.main.ISealImageDao;
import cfca.seal.exception.CodeException;
import cfca.seal.system.Env;
import cfca.seal.util.StringUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class SealImageDao
  implements ISealImageDao
{
  private static String tableName = "印模";
  private static final Logger log = LoggerFactory.getLogger("web_sys");

  @Resource
  private SmartDao smartDao;

  @Resource
  private PageDao pageDao;

  public boolean insert(SealImage sealmodel) throws CodeException { try { String sql = "INSERT INTO SEAL_IMAGE (ID, CODE, TYPE, NAME, ORGANIZATION_ID, SEAL_IMAGE_DATA, IMAGE_WIDTH, IMAGE_HEIGHT, CREATOR, CREATE_TIME, UPDATE_TIME) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, null)";

      Object[] args = { sealmodel.getId(), sealmodel.getCode(), Integer.valueOf(sealmodel.getType()), sealmodel.getName(), sealmodel.getOrganizationId(), sealmodel.getSealImageData(), Integer.valueOf(sealmodel.getImageWidth()), Integer.valueOf(sealmodel.getImageHeight()), sealmodel.getCreator(), new Date() };

      log.debug("insert seal_image sql: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1002", tableName + "表插入失败", e);
    } }

  public boolean update(SealImage sealmodel) throws CodeException
  {
    try
    {
      String sql = "UPDATE SEAL_IMAGE SET TYPE = ?, NAME = ?, ORGANIZATION_ID = ?, SEAL_IMAGE_DATA = ?, IMAGE_WIDTH = ?, IMAGE_HEIGHT = ?, UPDATE_TIME = ? WHERE ID = ?";

      Object[] args = { Integer.valueOf(sealmodel.getType()), sealmodel.getName(), sealmodel.getOrganizationId(), sealmodel.getSealImageData(), Integer.valueOf(sealmodel.getImageWidth()), Integer.valueOf(sealmodel.getImageHeight()), new Date(), sealmodel.getId() };

      log.debug("update seal_image sql by id: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1003", tableName + "表更新失败", e);
    }
  }

  public boolean deleteById(String id) throws CodeException
  {
    try {
      String sql = "DELETE FROM SEAL_IMAGE WHERE ID = ?";
      Object[] args = { id };
      log.debug("delete seal_image sql by id: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1004", tableName + "表删除失败", e);
    }
  }

  public SealImage getSealModelById(String id) throws CodeException
  {
    try {
      String sql = "SELECT * FROM SEAL_IMAGE WHERE ID = ?";
      Object[] args = { id };
      log.debug("select seal_image sql by id: " + sql + args);
      return (SealImage)this.smartDao.getBean(SealImage.class, sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public List<SealImage> getAll(String orgid) throws CodeException
  {
    try {
      String sql = "SELECT * FROM SEAL_IMAGE";
      if ((orgid != null) && (!orgid.isEmpty())) {
        sql = sql + " WHERE ORGANIZATION_ID = '" + orgid + "'";
      }
      log.debug("select seal_image sql by orgid: " + sql);
      return this.smartDao.getList(SealImage.class, sql);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public SealImage getSealModelByModelCode(String modelCode) throws CodeException
  {
    try {
      String sql = "SELECT * FROM SEAL_IMAGE WHERE CODE = ?";
      Object[] args = { modelCode };
      log.debug("select seal_image sql by code: " + sql + args);
      return (SealImage)this.smartDao.getBean(SealImage.class, sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public Page<SealImage> getPage(SealImage sealImage, String pageNo, String orgid) throws CodeException
  {
    try {
      List conditionList = new ArrayList();
      List args = new ArrayList();

      if (StringUtil.isNotEmpty(orgid)) {
        conditionList.add("ORGANIZATION_ID = ?");
        args.add(orgid);
      } else if (StringUtil.isNotEmpty(sealImage.getOrganizationId())) {
        conditionList.add("ORGANIZATION_ID = ?");
        args.add(sealImage.getOrganizationId());
      }

      if (StringUtil.isNotEmpty(sealImage.getCode())) {
        conditionList.add("CODE = ?");
        args.add(sealImage.getCode());
      }

      if (StringUtil.isNotEmpty(sealImage.getName())) {
        conditionList.add("NAME like ?");
        args.add("%" + sealImage.getName() + "%");
      }

      if (StringUtil.isNotEmpty(sealImage.getStartTime())) {
        if ("sqlite".equals(Env.databaseType)) {
          conditionList.add("DATETIME(substr(CREATE_TIME,0,11), 'unixepoch', 'localtime') >= DATETIME(?)");
          args.add(sealImage.getStartTime());
        } else if ("mysql".equals(Env.databaseType)) {
          conditionList.add("CREATE_TIME >= date_format(?,'%y-%m-%d %H:%i:%s')");
          args.add(sealImage.getStartTime());
        } else if ("oracle".equals(Env.databaseType)) {
          conditionList.add("CREATE_TIME >= to_date(?,'yyyy-mm-dd HH24:MI:SS')");
          args.add(sealImage.getStartTime());
        } else if ("db2".equals(Env.databaseType)) {
          conditionList.add("CREATE_TIME >= to_date(?,'yyyy-mm-dd hh24:mi:ss')");
          args.add(sealImage.getStartTime());
        }
      }

      if (StringUtil.isNotEmpty(sealImage.getEndTime())) {
        if ("sqlite".equals(Env.databaseType)) {
          conditionList.add("DATETIME(substr(CREATE_TIME,0,11), 'unixepoch', 'localtime') <= DATETIME(?)");
          args.add(sealImage.getEndTime());
        } else if ("mysql".equals(Env.databaseType)) {
          conditionList.add("CREATE_TIME <= date_format(?,'%y-%m-%d %H:%i:%s')");
          args.add(sealImage.getEndTime());
        } else if ("oracle".equals(Env.databaseType)) {
          conditionList.add("CREATE_TIME <= to_date(?,'yyyy-mm-dd HH24:MI:SS')");
          args.add(sealImage.getEndTime());
        } else if ("db2".equals(Env.databaseType)) {
          conditionList.add("CREATE_TIME <= to_date(?,'yyyy-mm-dd hh24:mi:ss')");
          args.add(sealImage.getEndTime());
        }
      }

      return this.pageDao.dispage(SealImage.class, "SEAL_IMAGE", conditionList, args.toArray(), pageNo, 20);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public List<SealImage> getAllForSelectShow(String orgid) throws CodeException
  {
    try {
      String sql = "SELECT ID, CODE, NAME FROM SEAL_IMAGE";
      if ((orgid != null) && (!orgid.isEmpty())) {
        sql = sql + " WHERE ORGANIZATION_ID = '" + orgid + "'";
      }
      sql = sql + " ORDER BY CODE ASC ";
      log.debug("select seal_image(id, code, name) sql by orgid: " + sql);
      return this.smartDao.getList(SealImage.class, sql);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public int getCountByOrganizationId(String organizationId) throws Exception
  {
    try {
      String sql = "SELECT COUNT(*) FROM SEAL_IMAGE WHERE ORGANIZATION_ID = ?";
      Object[] args = { organizationId };
      log.debug("select count sql by organizationid: " + sql + args);
      return this.smartDao.getCount(sql, args);
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }
}